package com.fish.anne.webapp.action;

import java.util.List;

import com.fish.anne.model.Auto;
import com.fish.anne.model.AutoBrand;
import com.fish.anne.service.AutoBrandManager;
import com.fish.anne.service.AutoManager;
import com.fish.anne.webapp.formbean.AutoSearch;
import com.opensymphony.xwork2.Preparable;

public class AutoAction extends BaseAction implements Preparable {
	private AutoManager autoManager;
	private List<Auto> autoes;
	private Auto auto;
	private Long autoId;
	private AutoSearch autoSearch;
	
	
	public AutoSearch getAutoSearch() {
		return autoSearch;
	}

	public void setAutoSearch(AutoSearch autoSearch) {
		this.autoSearch = autoSearch;
	}

	private AutoBrandManager autoBrandManager;

	private List<AutoBrand> autoBrands;

	public List<AutoBrand> getAutoBrands() {
		return autoBrands;
	}

	public void setAutoBrands(List<AutoBrand> autoBrands) {
		this.autoBrands = autoBrands;
	}

	public void setAutoBrandManager(AutoBrandManager autoBrandManager) {
		this.autoBrandManager = autoBrandManager;
	}

	public void setAutoManager(AutoManager autoManager) {
		this.autoManager = autoManager;
	}

	public List<Auto> getAutoes() {
		return autoes;
	}

	/**
	 * Grab the entity from the database before populating with request
	 * parameters
	 */
	public void prepare() {
		if (getRequest().getMethod().equalsIgnoreCase("post")) {
			// prevent failures on new
			String autoId = getRequest().getParameter("auto.autoId");
			if (autoId != null && !autoId.equals("")) {
				auto = autoManager.get(new Long(autoId));
			}
		}
	}

	public String list() {
		if(autoSearch==null){
			autoSearch = new AutoSearch();
		}
		Long count = this.autoManager.getCountOfSearch(autoSearch);
		this.autoSearch.setPageTotalRow(count);		
		//autoSearch.setPageRowPerPage(1);
		autoSearch.setPageCurrentPage(getPageCurrentPage());
		autoes = autoManager.findBySearch(autoSearch);
		
		return SUCCESS;
	}

	public void setAutoId(Long autoId) {
		this.autoId = autoId;
	}

	public Auto getAuto() {
		return auto;
	}

	public void setAuto(Auto auto) {
		this.auto = auto;
	}

	public String delete() {
		if (autoId == null) {
			return SUCCESS;
		}
		autoManager.remove(autoId);
		saveMessage(getText("auto.deleted"));

		return SUCCESS;
	}

	public String edit() {
		autoBrands = this.autoBrandManager.getAll();
		if (autoId != null) {
			auto = autoManager.get(autoId);
		} else {
			auto = new Auto();
		}

		return SUCCESS;
	}

	public String save() throws Exception {

		boolean isNew = (auto.getAutoId() == null);
		if(isNew){
			auto.setAutoCount(0L);
			auto.setBrand("");
			auto.setCreateTime(System.currentTimeMillis());
			auto.setLastModify(System.currentTimeMillis());
			auto.setInsure(0L);
			auto.setP1("");
			auto.setP2("");
			auto.setP3("");
			auto.setP4("");
			auto.setP5("");
			auto.setViewCount(0L);
		}
		if(file!=null){
			String link = this.upload();
			auto.setDefaultPic(link);
			auto.setSmallPic(link);
		}
		autoManager.save(auto);
		this.autoId = this.auto.getAutoId();
		String key = (isNew) ? "auto.added" : "auto.updated";
		saveMessage(getText(key));

		return SUCCESS;

	}

	public Long getAutoId() {
		return autoId;
	}
	
	 
}